if(!sourceType){
	var sourceType = {};
}

$.extend(sourceType,{
	sourceList:[]
	,errorTypeMap:{}
	,urlMap:{
		listSourceAndErrorType:"../recordSource/makeDbSelect.do"
		,createSource:"../recordSource/create.do"
		,updateSource:"../recordSource/update.do"
		,deleteSource:"../recordSource/delete.do"
		,createErrorType:"../errorType/create.do"
		,updateErrorType:"../errorType/update.do"
		,deleteErrorType:"../errorType/delete.do"
	}
	,sourceTemplate:function(data,index,container){
		var html = '<h3><a>&nbsp;'+data.name+'&nbsp;</a></h3>';
		html+='<div><button onclick="sourceType.editSource('+index+')">编辑来源</button>';
		html+='&nbsp;&nbsp;<button onclick="sourceType.deleteSource('+index+')">删除来源及其下故障类型</button>';
		html+='&nbsp;&nbsp;&nbsp;&nbsp;<button onclick="sourceType.addErrorType('+index+')">在此来源下添加故障类型</button>';
		html+='<table width="100%" border="0" cellpadding="0" cellspacing="0" class="tablelist">';
		html+='<thead><tr class="border-in"><th class="ui-widget-header">故障类型</th><th class="ui-widget-header">操作</th></tr></thead><tbody';
		$.each(sourceType.errorTypeMap[data.id],function(i,n){
			html+='<tr><td>'+n.name+'</td>';
			html+='<td><a href="javascript:void(0)" onclick="sourceType.editErrorType('+index+','+i+')"><li class="ui-state-default ui-corner-all" title="编辑"><span class="ui-icon ui-icon-wrench"></span></li></a>';
			html+='<a href="javascript:void(0)" onclick="sourceType.deleteErrorType('+index+','+i+')"><li class="ui-state-default ui-corner-all" title="删除"><span class="ui-icon ui-icon-trash"></span></li></a></td></tr>';
		});
		html+='</tbody></table></div>';
		return html;
	}
	,addSource:function(){
		sourceType.toAddOrEdit('#sourceDiv',{},"添加来源",sourceType.urlMap["createSource"],"添加来源成功");
	}
	,editSource:function(index){
		sourceType.toAddOrEdit('#sourceDiv',sourceType.sourceList[index],"修改来源",sourceType.urlMap["updateSource"],"修改来源成功",function(){
			sourceType.openTo(index);
		});
	}
	,deleteSource:function(index){
		var source = sourceType.sourceList[index];
		commonUtil.showConfirm("确认删除来源【"+source.name+"】","这要删除这个来源，和来源下面的所有故障类型，确定吗？",{"确定":function(){
			commonUtil.jsonPost(sourceType.urlMap["deleteSource"],{"pojo.id":source.id},function(data){
				$(commonUtil.confirmId).dialog("close");
				commonUtil.showAlert("完成","删除成功");
				sourceType.search();
			});
		}});
	}
	,addErrorType:function(sourceIndex){
		var source = sourceType.sourceList[sourceIndex];
		$("#eSourceName").html('').html(source.name);
		sourceType.toAddOrEdit('#errorTypeDiv',{"sourceFk":source.id},"添加故障类型",sourceType.urlMap["createErrorType"],"添加故障类型成功",function(){
			sourceType.openTo(sourceIndex);
		});
	}
	,editErrorType:function(sourceIndex,errorTypeIndex){
		var source = sourceType.sourceList[sourceIndex];
		var errorType = sourceType.errorTypeMap[source.id][errorTypeIndex];
		$("#eSourceName").html('').html(source.name);
		sourceType.toAddOrEdit('#errorTypeDiv',errorType,"修改故障类型",sourceType.urlMap["updateErrorType"],"修改故障类型成功",function(){
			sourceType.openTo(sourceIndex);
		});
	}
	,deleteErrorType:function(sourceIndex,errorTypeIndex){
		var errorType = sourceType.errorTypeMap[sourceType.sourceList[sourceIndex].id][errorTypeIndex];
		commonUtil.showConfirm("确认删除故障类型【"+errorType.name+"】","删除这个故障类型，确定吗？",{"确定":function(){
			commonUtil.jsonPost(sourceType.urlMap["deleteErrorType"],{"pojo.id":errorType.id},function(data){
				$(commonUtil.confirmId).dialog("close");
				commonUtil.showAlert("完成","删除成功");
				sourceType.search(function(){
					sourceType.openTo(sourceIndex);
				});
			});
		}});
	}
	,toAddOrEdit:function(div,obj,title,url,optmsg,func){
		commonUtil.setInputValues(div,{},"pojo.");
		commonUtil.setInputValues(div,obj,"pojo.");
		$(div).dialog({
			show: "fade",
			title:title,
			modal: true,
			width:"350",
			height:"200",
			buttons: {
				"保存":function(){
					var params = commonUtil.getInputValues(div);
					commonUtil.jsonPost(url,params,function(data){
						commonUtil.showAlert("完成",optmsg);
						sourceType.search(func);
						$(div).dialog("close");
					});
				}
				,"取消":function(){ $(this).dialog("close"); } 
			}
		});
	}
	,delRecord:function(i){
		commonUtil.showConfirm("确认","确定删除这条工单？",{"确定":function(){
			commonUtil.jsonPost(recordSearch.urlMap["deleteRecord"],{"pojo.id":recordSearch.recordList[i].id},function(data){
				$(commonUtil.confirmId).dialog("close");
				commonUtil.showAlert("完成","删除成功");
				recordSearch.search();
			});
		}});
	}
	,search:function(func){
		$("#sourceOuter").empty().append('<div id="source_list"></div>');
		commonUtil.jsonPost(recordManage.urlMap["listSourceAndErrorType"],{},function(data){
			sourceType.errorTypeMap=data.errorTypeMap;
			sourceType.sourceList=data.pojoList;
			commonUtil.generateDataTable(data.pojoList,"source_list",sourceType.sourceTemplate);
			$("#source_list").accordion({collapsible:true});
			$("button").button();
			if(func){
				try{func();}catch(e){}
			}
		},"#source_list");
	}
	,openTo:function(index){
		if (index > 0) {
			$("#source_list h3")[sourceIndex].click();
		}
	}
});


$(document).ready(function(){
	$(":text").attr("class","text ui-widget ui-widget-content ui-corner-all");
	sourceType.search();
});

